package com.sk.bilibili.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.bilibili.bean.Dynamic;
import com.sk.bilibili.event.BiliBiliEvent;
import com.sk.bilibili.other.R;
import com.sk.bilibili.utils.BSessionUtils;
import com.sk.bilibili.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import com.sk.bilibili.bean.Note;
import com.sk.bilibili.mapper.NoteMapper;
import org.springframework.web.multipart.MultipartFile;

import java.util.regex.Pattern;

/**
 * @author 邵康
 * @date 2021/11/24 19:22
 */
@Service
public class NoteService extends ServiceImpl<NoteMapper, Note> {

    @Autowired
    private MinioService minioService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private PraiseService praiseService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    private static final String IMAGE_PATH = "note/image/%s.jpg";
    private static final Pattern IMAGE_PATTERN = Pattern.compile("/(note/image/.*jpg)");

    public R<Void> add(Note note) {
        int memberId = BSessionUtils.getMemberId();
        note.setMemberId(memberId);
        note.setUrls(buildUrls(note.getImages()));
        save(note);
        eventPublisher.publishEvent(new BiliBiliEvent<>(this, note));
        return R.success();
    }

    private String[] buildUrls(MultipartFile[] images) {
        if(images==null)return new String[0];
        String[] res = new String[images.length];
        for (int i = 0; i < res.length; i++) {
            res[i] = minioService.add(images[i], IMAGE_PATH);
        }
        return res;
    }

    public R<Void> delete(Integer id) {
        Note note = getById(id);
        String[] urls = note.getUrls();
        for (String url : urls) {
            minioService.delete(url, IMAGE_PATTERN);
        }
        removeById(id);
        return R.success();
    }

    public R<Void> update(Note note) {
        updateById(note);
        return R.success();
    }

    public R<IPage<Note>> page(Integer page, Integer size) {
        return R.success(page(new Page<>(page, size), null));
    }

    public R<Note> getNote(Integer id) {
        return R.success(getWarpNote(id));
    }

    public Note getWarpNote(Integer id) {
        Note note = getById(id);
        note.getExtra().addExtra(memberService.getById(note.getMemberId()));
        int memberId = BSessionUtils.getMemberId();
        note.getExtra().put("praise", praiseService.hasPraise(memberId, 2, id));
        return note;
    }

    public void changeNum(String column, Integer num, Integer id) {
        redisUtils.changeNum("Note-"+id,column,num);
    }

    @EventListener(condition = "event.type == 'com.sk.bilibili.service.DynamicService.delete'")
    public void handleDynamicDelete(BiliBiliEvent<Dynamic> event){
        Dynamic dynamic = event.getData();
        if(dynamic.getType()!=2)return;
        delete(dynamic.getTargetId());
    }

}
